package cn.iocoder.yudao.module.lfd.controller.admin.customer;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.module.system.api.oauth2.dto.OAuth2AccessTokenRespDTO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.security.PermitAll;
import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.lfd.controller.admin.customer.vo.*;
import cn.iocoder.yudao.module.lfd.dal.dataobject.customer.CustomerDO;
import cn.iocoder.yudao.module.lfd.convert.customer.CustomerConvert;
import cn.iocoder.yudao.module.lfd.service.customer.CustomerService;

@Tag(name = "管理后台 - 顾客")
@RestController
@RequestMapping("/lfd/customer")
@Validated
public class CustomerController {

    @Resource
    private CustomerService customerService;

    @PostMapping("/create")
    @Operation(summary = "创建顾客")
    @PreAuthorize("@ss.hasPermission('lfd:customer:create')")
    public CommonResult<Long> createCustomer(@Valid @RequestBody CustomerCreateReqVO createReqVO) {
        return success(customerService.createCustomer(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新顾客")
    @PreAuthorize("@ss.hasPermission('lfd:customer:update')")
    public CommonResult<Boolean> updateCustomer(@Valid @RequestBody CustomerUpdateReqVO updateReqVO) {
        customerService.updateCustomer(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除顾客")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('lfd:customer:delete')")
    public CommonResult<Boolean> deleteCustomer(@RequestParam("id") Long id) {
        customerService.deleteCustomer(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得顾客")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('lfd:customer:query')")
    public CommonResult<CustomerRespVO> getCustomer(@RequestParam("id") Long id) {
        CustomerDO customer = customerService.getCustomer(id);
        return success(CustomerConvert.INSTANCE.convert(customer));
    }

    @GetMapping("/list")
    @Operation(summary = "获得顾客列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('lfd:customer:query')")
    public CommonResult<List<CustomerRespVO>> getCustomerList(@RequestParam("ids") Collection<Long> ids) {
        List<CustomerDO> list = customerService.getCustomerList(ids);
        return success(CustomerConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得顾客分页")
    @PreAuthorize("@ss.hasPermission('lfd:customer:query')")
    public CommonResult<PageResult<CustomerRespVO>> getCustomerPage(@Valid CustomerPageReqVO pageVO) {
        PageResult<CustomerDO> pageResult = customerService.getCustomerPage(pageVO);
        return success(CustomerConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出顾客 Excel")
    @PreAuthorize("@ss.hasPermission('lfd:customer:export')")
    @OperateLog(type = EXPORT)
    public void exportCustomerExcel(@Valid CustomerExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CustomerDO> list = customerService.getCustomerList(exportReqVO);
        // 导出 Excel
        List<CustomerExcelVO> datas = CustomerConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "顾客.xls", "数据", CustomerExcelVO.class, datas);
    }


    @GetMapping("/mini-get")
    @Operation(summary = "通过openId获取用户信息")
    @PermitAll
    public CommonResult<CustomerExcelVO> getCustomerByOpenid(String openid) {
        CustomerExportReqVO vo=new CustomerExportReqVO();
        List<CustomerDO> list = customerService.getCustomerList(vo.setOpenid(openid));
        // 导出 Excel
        List<CustomerExcelVO> datas = CustomerConvert.INSTANCE.convertList02(list);
        return success(CollectionUtil.getFirst(datas));
    }

    @GetMapping("/mini-weixin-login")
    @Operation(summary = "通过openid登录获取token")
    @PermitAll
    public CommonResult<OAuth2AccessTokenRespDTO> weixinMiniLogin(@Valid @NotBlank String openId) {
        OAuth2AccessTokenRespDTO respDTO = customerService.createAccessToken(openId);
        return success(respDTO);
    }

}
